Skewled Posted August 5, 2010 Share Posted August 5, 2010 So I have the following and for some reason or another it's not wanting to work with me. If I do the query directly in phpmyadmin it will return the number, however for some reason when I throw it into a variable it won't return anything. I have a feeling it's because of the text string? How do you properly represent a string of text that has a space between it? 1st query get's the name from another table: $query = "SELECT name FROM info WHERE userdid = 4"; $data = mysqli_query($dbc, $query); $row = mysqli_fetch_array($data); $tname = $row['name']; Table: testing rows: number name data: 4 Testing This $query1 = "SELECT number FROM testing WHERE name = '" . $tname . "'"; $data1 = mysqli_query($dbc, $query1); $row1 = mysqli_query($data1); $numreturn = $row1['number']; // Do stuff Quote Link to comment https://forums.phpfreaks.com/topic/209916-query-assistance/ Share on other sites More sharing options...
wildteen88 Posted August 5, 2010 Share Posted August 5, 2010 Both codes are correct, except in your second code block this line $row1 = mysqli_query($data1); Should be $row1 = mysqli_fetch_array($data1); Note, You call mysqli_query to execute the query you defined. To get the data returned from the query you use mysqli_fetch_array Quote Link to comment https://forums.phpfreaks.com/topic/209916-query-assistance/#findComment-1095653 Share on other sites More sharing options...
Skewled Posted August 5, 2010 Author Share Posted August 5, 2010 my apologies that was a typo on my part it is actually mysqli_fetch_array($data1); So I'm wondering why it's not taking the $tname value Quote Link to comment https://forums.phpfreaks.com/topic/209916-query-assistance/#findComment-1095661 Share on other sites More sharing options...
Skewled Posted August 5, 2010 Author Share Posted August 5, 2010 What I should be asking is how do you pass a variable with a value of Test's Suck into a query. Currently the query sees only 's Suck and will return 0 results. So how do I pass Test's Suck to the new query? Quote Link to comment https://forums.phpfreaks.com/topic/209916-query-assistance/#findComment-1095672 Share on other sites More sharing options...
wildteen88 Posted August 5, 2010 Share Posted August 5, 2010 As you're passing a string to mysql you'll need to wrap it within quotes. Which is what you have already done. $query1 = "SELECT number FROM testing WHERE name = '" . $tname . "'"; Notice the single quotes before/after the double quotes. That should work regardless of the spaces. How are your defining $tname? And what is its value? Quote Link to comment https://forums.phpfreaks.com/topic/209916-query-assistance/#findComment-1095674 Share on other sites More sharing options...
Skewled Posted August 5, 2010 Author Share Posted August 5, 2010 $tname is pulled from the 1st query assigned as: $tname = $row['name']; so effectively: $tname = "Test's Suck"; error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 's Suck'' at line 1 when I put the query in phpmyadmin: SELECT number FROM testing WHERE name = "Test's Suck" it retrieves the value fine so that's why I'm confused lol Quote Link to comment https://forums.phpfreaks.com/topic/209916-query-assistance/#findComment-1095676 Share on other sites More sharing options...
wildteen88 Posted August 5, 2010 Share Posted August 5, 2010 Ahh, its the quote after Test that is causing the issue. You'll want to use mysql_real_escape_string $query1 = "SELECT number FROM testing WHERE name = '" . mysql_real_escape_string($tname) . "'"; Quote Link to comment https://forums.phpfreaks.com/topic/209916-query-assistance/#findComment-1095679 Share on other sites More sharing options...
Skewled Posted August 5, 2010 Author Share Posted August 5, 2010 Thank you! mysqli_real_escape_string($dbc, trim($tname)) did the trick! Quote Link to comment https://forums.phpfreaks.com/topic/209916-query-assistance/#findComment-1095685 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.