Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/209916-query-assistance/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/209916-query-assistance/#findComment-1095653
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/209916-query-assistance/#findComment-1095674
Share on other sites

$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

 

Link to comment
https://forums.phpfreaks.com/topic/209916-query-assistance/#findComment-1095676
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.