1

I'm having troubles with a PHP code. The problems come when I execute a SQL query with a PHP variable inside it. The result doesn't shows anything, and the field in the database is not empty. Tried out with a static id (not variable) and the query works fine.

Do you know where I'm doing it wrong?

The query code:

$consultaVideoSeleccionado1 = mysql_query("SELECT * FROM  videos WHERE  idvideo = `'.$videoSeleccionado.'`;", $conexion);
5
  • What are you using backticks (`) for? If your $videoSeleccionado value is a string, use single quotes, not backticks!!! Commented May 8, 2011 at 15:16
  • Learn to handle errors returned by MySQL, so that you can see why it complains about your queries when they don't work. Learning how to handle MySQL errors now will help save you a lot of time in the future. Commented May 8, 2011 at 15:18
  • The backticks was because i tried it out with de phpmyadmin and generated em. Commented May 8, 2011 at 15:33
  • phpmyadmin should only ever use backticks around table/column names, never around data values Commented May 8, 2011 at 15:35
  • If $videoSelecciono should be a number make sure that it is. Oherwise please make sure that you are using mysql_real_escape_string php.net/manual/en/function.mysql-real-escape-string.php or prepared statements. This helps prevent SQL injection attacks en.wikipedia.org/wiki/Sql_injection Commented May 9, 2011 at 13:27

3 Answers 3

1

Try this:

$consultaVideoSeleccionado1 = mysql_query("SELECT * FROM  videos WHERE  idvideo = `'.$videoSeleccionado.'`;", $conexion) or die(mysql_error());

That will give you an error message.

The problem is that you use both ` and ' as escape characters as the same time.

$consultaVideoSeleccionado1 = mysql_query("SELECT * FROM  videos WHERE  idvideo = '$videoSeleccionado';", $conexion);

should work.

Often things will be more clear when you echo the query so you can see the final result.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm getting an empty result anyway. The thing that i can't believe is the echo with the entire query returns what it have to return, but i'm still getting the empty result... do you know why its happening? Thank you very much.
1

you are using double quotes on your query so there is no need for the dot . operator $consultaVideoSeleccionado1 = mysql_query("SELECT * FROM videos WHERE idvideo = '$videoSeleccionado'", $conexion);

Comments

1

You are connecting string wrong! You are using ' single quote, but you should use double/none.

Try this query:

mysql_query("SELECT * FROM  `videos` WHERE  `idvideo` = '$videoSeleccionado';", $conexion)

Or

mysql_query("SELECT * FROM  `videos` WHERE  `idvideo` = '{$videoSeleccionado}';", $conexion)

Or

$q = "SELECT * FROM  `videos` WHERE  `idvideo` = '%s';";
mysql_query(sprintf($q, $videoSeleccionado), $conexion)

Edit:

If it's still not working problem can be in query, try checking if it is using mysql_error()(1) or try dumping query(2).

Example(1):

mysql_query("SELECT * FROM  `videos` WHERE  `idvideo` = '".$videoSeleccionado."';", $conexion) or die(mysql_error());

Example(2):

$q = "SELECT * FROM videos WHERE idvideo = '$videoSeleccionado';"; var_dump($q); mysql_query($q, $conexion)

1 Comment

I'm getting an empty result anyway. The thing that i can't believe is the echo with the entire query returns what it have to return, but i'm still getting the empty result... do you know why its happening? Thank you very much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.