Mysqli多个带有临时表和存储函数的预处理语句。(Mysqli multiple prepared statements with temp table and stored function. Last prepared statement not binding)
# I am using a combination of php prepared statements with mysql. I have created stored functions which work fine. I also am creating a temp table. However when I hardcode the last prepared query my result table is sorted correctly but when I try to use prepared bind parameters it doesn't sort correctly. Here is the flow of what is going on # $createTempTable = ' CREATE TEMPORARY TABLE IF NOT EXISTS table2 AS ( SELECT st.Songs_ID, st.Num_Of_Votes, st.Song_Title, st.Date_Released, st.MusicLink, st.ytVideoID, " " AS artists, " " AS genres FROM Songs_Table st INNER JOIN Songs_Genres_Crossover sgc ON sgc.Song_ID = st.Songs_ID INNER JOIN Song_Genres sg ON sgc.Genre_ID = sg.Genre_ID AND sg.Genre_Label = ? WHERE st.Date_Released >= ? && st.Date_Released <= ? ); '; $stmt1 = $mysqli -> prepare("$createTempTable"); $stmt1->bind_param('sss', $genreLabelLowercase, $startTime, $endTime); $stmt1->execute(); $stmt1->close(); //this calls stored function and requires no params $updateTempTable = ' SELECT populateSongIdsWithArtistsAndGenres(); '; $stmt2 = $mysqli -> prepare("$updateTempTable"); $stmt2->execute(); $stmt2->close(); //this is the function that doesn't seem to bind correctly //notice if i hardcode this and do not use ? in it the query is executed fine $getTempTableResult = ' SELECT Songs_ID, Num_Of_Votes, Song_Title, Date_Released, MusicLink, ytVideoID, artists, genres FROM table2 ORDER BY ? LIMIT ? , ? ; '; $stmt3 = $mysqli -> prepare("$getTempTableResult"); $topVotedOrderBy = 'Num_Of_Votes DESC, artists ASC, Song_Title ASC'; $firstSong = 0; $songsToUse = 100; $stmt3->bind_param('sii', $topVotedOrderBy, $firstSong, $songsToUse); $stmt3->execute(); /* Bind results */ $stmt3 -> bind_result($songId, $numOfVotes, $songTitle, $dateReleased, $musicLink, $ytVidId, $artists, $genres); while ($stmt3 -> fetch()) { //use results } $stmt3->close();
我希望这是帮助所需的所有信息。 我需要弄清楚为什么stmt3没有正确执行。 谢谢! 我正在寻找mysqli_multi_query,但我希望以这种方式使查询工作,在我冒险使用multi_query之前用预备语句进行保护。
Thanks guys I am using the query in this way as a workaround. It is messier but it works fine # $createTempTable = ' CREATE TEMPORARY TABLE IF NOT EXISTS table2 AS ( SELECT st.Songs_ID, st.Num_Of_Votes, st.Song_Title, st.Date_Released, st.MusicLink, st.ytVideoID, " " AS artists, " " AS genres FROM Songs_Table st INNER JOIN Songs_Genres_Crossover sgc ON sgc.Song_ID = st.Songs_ID INNER JOIN Song_Genres sg ON sgc.Genre_ID = sg.Genre_ID AND sg.Genre_Label = ? WHERE st.Date_Released >= ? && st.Date_Released <= ? ); '; $stmt1 = $mysqli -> prepare("$createTempTable"); $stmt1->bind_param('sss', $genreLabelLowercase, $startTime, $endTime); $stmt1->execute(); $stmt1->close(); $updateTempTable = ' SELECT populateSongIdsWithArtistsAndGenres(); '; $stmt2 = $mysqli -> prepare("$updateTempTable"); $stmt2->execute(); $stmt2->close(); if($sort === 'Date Released(Newest First)') { //newest released $dateReleasedNewestFirstOrderBy = 'Date_Released DESC, artists ASC, Song_Title ASC'; $getTempTableResult = ' SELECT Songs_ID, Num_Of_Votes, Song_Title, Date_Released, MusicLink, ytVideoID, artists, genres FROM table2 ORDER BY '.$dateReleasedNewestFirstOrderBy.' LIMIT '.$firstSong.' , '.$songsToUse.' ; '; } else if ($sort == 'Artist(A-Z)') {//all artists a to z //artist a-z $artistAToZOrderBy = 'artists ASC, Song_Title ASC'; $getTempTableResult = ' SELECT Songs_ID, Num_Of_Votes, Song_Title, Date_Released, MusicLink, ytVideoID, artists, genres FROM table2 ORDER BY '.$artistAToZOrderBy.' LIMIT '.$firstSong.' , '.$songsToUse.' ; '; } else if ($sort == 'Title(A-Z)') { //title a-z $songTitlesAToZOrderBy = 'Song_Title ASC, artists ASC'; $getTempTableResult = ' SELECT Songs_ID, Num_Of_Votes, Song_Title, Date_Released, MusicLink, ytVideoID, artists, genres FROM table2 ORDER BY '.$songTitlesAToZOrderBy.' LIMIT '.$firstSong.' , '.$songsToUse.' ; '; } else { //top voted $topVotedOrderBy = 'Num_Of_Votes DESC, artists ASC, Song_Title ASC'; $getTempTableResult = ' SELECT Songs_ID, Num_Of_Votes, Song_Title, Date_Released, MusicLink, ytVideoID, artists, genres FROM table2 ORDER BY '.$topVotedOrderBy.' LIMIT '.$firstSong.' , '.$songsToUse.' ; '; } $stmt3 = $mysqli -> prepare("$getTempTableResult"); $stmt3->execute(); $stmt3 -> bind_result($songId, $numOfVotes, $songTitle, $dateReleased, $musicLink, $ytVidId, $artists, $genres); while ($stmt3 -> fetch()) { //use results } $stmt3->close();
# I am using a combination of php prepared statements with mysql. I have created stored functions which work fine. I also am creating a temp table. However when I hardcode the last prepared query my result table is sorted correctly but when I try to use prepared bind parameters it doesn't sort correctly. Here is the flow of what is going on # $createTempTable = ' CREATE TEMPORARY TABLE IF NOT EXISTS table2 AS ( SELECT st.Songs_ID, st.Num_Of_Votes, st.Song_Title, st.Date_Released, st.MusicLink, st.ytVideoID, " " AS artists, " " AS genres FROM Songs_Table st INNER JOIN Songs_Genres_Crossover sgc ON sgc.Song_ID = st.Songs_ID INNER JOIN Song_Genres sg ON sgc.Genre_ID = sg.Genre_ID AND sg.Genre_Label = ? WHERE st.Date_Released >= ? && st.Date_Released <= ? ); '; $stmt1 = $mysqli -> prepare("$createTempTable"); $stmt1->bind_param('sss', $genreLabelLowercase, $startTime, $endTime); $stmt1->execute(); $stmt1->close(); //this calls stored function and requires no params $updateTempTable = ' SELECT populateSongIdsWithArtistsAndGenres(); '; $stmt2 = $mysqli -> prepare("$updateTempTable"); $stmt2->execute(); $stmt2->close(); //this is the function that doesn't seem to bind correctly //notice if i hardcode this and do not use ? in it the query is executed fine $getTempTableResult = ' SELECT Songs_ID, Num_Of_Votes, Song_Title, Date_Released, MusicLink, ytVideoID, artists, genres FROM table2 ORDER BY ? LIMIT ? , ? ; '; $stmt3 = $mysqli -> prepare("$getTempTableResult"); $topVotedOrderBy = 'Num_Of_Votes DESC, artists ASC, Song_Title ASC'; $firstSong = 0; $songsToUse = 100; $stmt3->bind_param('sii', $topVotedOrderBy, $firstSong, $songsToUse); $stmt3->execute(); /* Bind results */ $stmt3 -> bind_result($songId, $numOfVotes, $songTitle, $dateReleased, $musicLink, $ytVidId, $artists, $genres); while ($stmt3 -> fetch()) { //use results } $stmt3->close();
I hope this is all the info needed to help. I need to figure out why stmt3 is not executing correctly. Thanks! I was looking int mysqli_multi_query but I would like to make the query work this way which protects with the prepared statements before i venture to multi_query.
Thanks guys I am using the query in this way as a workaround. It is messier but it works fine # $createTempTable = ' CREATE TEMPORARY TABLE IF NOT EXISTS table2 AS ( SELECT st.Songs_ID, st.Num_Of_Votes, st.Song_Title, st.Date_Released, st.MusicLink, st.ytVideoID, " " AS artists, " " AS genres FROM Songs_Table st INNER JOIN Songs_Genres_Crossover sgc ON sgc.Song_ID = st.Songs_ID INNER JOIN Song_Genres sg ON sgc.Genre_ID = sg.Genre_ID AND sg.Genre_Label = ? WHERE st.Date_Released >= ? && st.Date_Released <= ? ); '; $stmt1 = $mysqli -> prepare("$createTempTable"); $stmt1->bind_param('sss', $genreLabelLowercase, $startTime, $endTime); $stmt1->execute(); $stmt1->close(); $updateTempTable = ' SELECT populateSongIdsWithArtistsAndGenres(); '; $stmt2 = $mysqli -> prepare("$updateTempTable"); $stmt2->execute(); $stmt2->close(); if($sort === 'Date Released(Newest First)') { //newest released $dateReleasedNewestFirstOrderBy = 'Date_Released DESC, artists ASC, Song_Title ASC'; $getTempTableResult = ' SELECT Songs_ID, Num_Of_Votes, Song_Title, Date_Released, MusicLink, ytVideoID, artists, genres FROM table2 ORDER BY '.$dateReleasedNewestFirstOrderBy.' LIMIT '.$firstSong.' , '.$songsToUse.' ; '; } else if ($sort == 'Artist(A-Z)') {//all artists a to z //artist a-z $artistAToZOrderBy = 'artists ASC, Song_Title ASC'; $getTempTableResult = ' SELECT Songs_ID, Num_Of_Votes, Song_Title, Date_Released, MusicLink, ytVideoID, artists, genres FROM table2 ORDER BY '.$artistAToZOrderBy.' LIMIT '.$firstSong.' , '.$songsToUse.' ; '; } else if ($sort == 'Title(A-Z)') { //title a-z $songTitlesAToZOrderBy = 'Song_Title ASC, artists ASC'; $getTempTableResult = ' SELECT Songs_ID, Num_Of_Votes, Song_Title, Date_Released, MusicLink, ytVideoID, artists, genres FROM table2 ORDER BY '.$songTitlesAToZOrderBy.' LIMIT '.$firstSong.' , '.$songsToUse.' ; '; } else { //top voted $topVotedOrderBy = 'Num_Of_Votes DESC, artists ASC, Song_Title ASC'; $getTempTableResult = ' SELECT Songs_ID, Num_Of_Votes, Song_Title, Date_Released, MusicLink, ytVideoID, artists, genres FROM table2 ORDER BY '.$topVotedOrderBy.' LIMIT '.$firstSong.' , '.$songsToUse.' ; '; } $stmt3 = $mysqli -> prepare("$getTempTableResult"); $stmt3->execute(); $stmt3 -> bind_result($songId, $numOfVotes, $songTitle, $dateReleased, $musicLink, $ytVidId, $artists, $genres); while ($stmt3 -> fetch()) { //use results } $stmt3->close();
原文:https://stackoverflow.com/questions/24353449
最满意答案
好吧,也许有希望,这可以帮助别人。 没有雄辩的方法来做到这一点,但我想出了一种方法来创建某种解决方案。
首先,我们从服务器返回Xml Stream,并将其转换为XmlDocument。 然后我们挑选出我想修复的所有XmlNode并创建了一个XmlNodeList。 然后,我们逐步执行XmlNodeList并更正每个ChildNode。 我们替换ChildNodes,并返回InnerXML的String。
然后,我将一个函数写入Parse XML Stream,并将其写入我想要的对象中。
所以我的财产看起来像:
<XmlArray("reqs"), XmlArrayItem("req")> _ Public Property ReqsList() As List(Of ReqItem) Get Return Me._reqsList End Get Set(value As List(Of ReqItem)) Me._reqsList = value End Set End Property
我的FixInstrumentReqs函数看起来像:
Public Shared Function FixInstrumentReqs(stream As String) As String Dim xml As New XmlDocument() xml.LoadXml(stream) Dim xmlNodes As XmlNodeList = xml.SelectNodes("/xml/ServiceResponse/List/instrument/reqs") For Each x As XmlElement In xmlNodes For i As Integer = 0 To x.ChildNodes.Count Step 1 Dim y As XmlElement = x.ChildNodes.Item(i) If IsNothing(y) = False Then Dim _new As XmlElement = xml.CreateElement("req") Dim attr As XmlAttribute = xml.CreateAttribute("name") attr.Value = y.Name _new.SetAttributeNode(attr) _new.InnerText = y.InnerText y.ParentNode.ReplaceChild(_new, y) End If Next Next stream = xml.InnerXml.ToString() Return stream End Function
希望,这有助于其他人。
Okay, so hopefully, maybe, this can help someone else. There was no eloquent way to do this, but I came up with a way to create some sort of solution.
First we took the Xml Stream back from the server, and converted it into an XmlDocument. Then we picked out all the XmlNodes that I want to fix and created an XmlNodeList. We, then, step through the XmlNodeList and correct each ChildNode. We replace the ChildNodes, and return back a String of the InnerXML.
I then have a function I've written to Parse XML Stream's into the objects I want.
So my Property looks like:
<XmlArray("reqs"), XmlArrayItem("req")> _ Public Property ReqsList() As List(Of ReqItem) Get Return Me._reqsList End Get Set(value As List(Of ReqItem)) Me._reqsList = value End Set End Property
And my FixInstrumentReqs Function looks like:
Public Shared Function FixInstrumentReqs(stream As String) As String Dim xml As New XmlDocument() xml.LoadXml(stream) Dim xmlNodes As XmlNodeList = xml.SelectNodes("/xml/ServiceResponse/List/instrument/reqs") For Each x As XmlElement In xmlNodes For i As Integer = 0 To x.ChildNodes.Count Step 1 Dim y As XmlElement = x.ChildNodes.Item(i) If IsNothing(y) = False Then Dim _new As XmlElement = xml.CreateElement("req") Dim attr As XmlAttribute = xml.CreateAttribute("name") attr.Value = y.Name _new.SetAttributeNode(attr) _new.InnerText = y.InnerText y.ParentNode.ReplaceChild(_new, y) End If Next Next stream = xml.InnerXml.ToString() Return stream End Function
Hopefully, that helps someone else out.
相关问答
更多-
VB和C语言哪个难一些?[2023-06-26]
vb -
TCP/IP模型是一个________。[2023-10-02]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
好吧,也许有希望,这可以帮助别人。 没有雄辩的方法来做到这一点,但我想出了一种方法来创建某种解决方案。 首先,我们从服务器返回Xml Stream,并将其转换为XmlDocument。 然后我们挑选出我想修复的所有XmlNode并创建了一个XmlNodeList。 然后,我们逐步执行XmlNodeList并更正每个ChildNode。 我们替换ChildNodes,并返回InnerXML的String。 然后,我将一个函数写入Parse XML Stream,并将其写入我想要的对象中。 所以我的财产看起来像 ...
-
我发现它是在我的班级Player 。 我用过的东西: Dim Name As String Dim Score As Double 什么工作: Public Name As String Public Score As Double 我应该做的: Public Property Name As String Public Property Score As Double 我被教导在使用Java进行编码时从不将变量设置为“public”,并且我不知道Visual Basic中存在Property 。 I ...
-
对于VB.Net: Dim x As Object = JsonConvert.DeserializeObject(of Object)(json) For VB.Net: Dim x As Object = JsonConvert.DeserializeObject(of Object)(json)
-
在C#中,解决方案是将NonSerializedAttribute应用于事件的后台字段,使用属性上的field:前缀。 不幸的是,VB不支持该field:技巧,所以你需要使事件的支持字段显式化,并将NonSerializedAttribute应用于该字段。 Rocky Lhotka为此提供了示例代码。 不幸的是,它看起来很冗长乏味,因为现在你必须说明每个事件的添加,删除和提升方法。 In C#, the solution is to apply the NonSerializedAttribute to t ...
-
我们假设您有以下类型: Public Class Parent Public Property ParentId As Integer Public Property ParentName As String Public ReadOnly Property Children As New List(Of Child) End Class Public Class Child Public Property ChildId As Integer Pub ...
-
如果您将json作为数组获取,为什么不将它反序列化为Array? 然后,您可以使用.ToList()方法将数组转换为List 。 你的课应该是这样的: public class Message { public int id { get; set; } public int previous { get; set; } public int last_update { get; set; } public string html { get; set; } } public ...
-
在VB.NET中使用XMLSerializer序列化通用集合(Serializing a generic collection with XMLSerializer in VB.NET)[2023-01-26]
问题是您已在Module Module1中包含类User。 该错误消息表明此模块的可访问性不公开。 因此,User的实际可访问性不是公共的,因为它嵌套在Module中。 将外部模块的定义更改为公共或将类用户移动到模块外部,它应该可以解决您的问题。 编辑 正如几位人士指出的那样,实现这一目标的最简洁方法是将User类放入其自己的文件中。 The problem is that you have included the class User inside of the Module Module1. The ...