Jump to content

PHP + MySQL

Go to solution Solved by fizzlesticks,
3 minutes ago, WildCAt said:

 

It's telling you "No database selected" which probably means your call to mysql_select_db() is failing. Add another echo mysql_error() after selecting your DB to see why it isn't working. 

Hello! ^^

I have such code

I'm using xampp.

<?php

header('Content-Type: text/html; charset=UTF-8');

mb_internal_encoding('UTF-8');
mb_http_output('UTF-8');
mb_http_input('UTF-8');
mb_regex_encoding('UTF-8'); 


$dbHost="localhost"; 
$dbName="people";
$dbUser="people"; 
$dbPass="Labas123"; 

$myConnect = mysql_connect($dbHost,$dbName,$dbPass);
mysql_select_db($dbName,$myConnect); 

$sql = mysql_query('SELECT * 
FROM  `table` 
WHERE  `LYTIS` =  "V"
AND  `GIMIMO_DATA` <  "1940-01-01"');

echo mysql_error(); 
$data = mysql_num_rows($sql); 

echo 'Vyrų, gimusiu iki 1940-01-01 kiekis: '.$data;

?>

The problem is here 

$sql = mysql_query('SELECT * FROM  `table` WHERE  `LYTIS` =  "V" AND  `GIMIMO_DATA` <  "1940-01-01"');

IT WORKS in phpmyadmin, SQL tab. It shows exactly what I need

However, as I see, php understands it a comment

Ok, lets remove '

$sql = mysql_query(SELECT * FROM  `table` WHERE  `LYTIS` =  "V" AND  `GIMIMO_DATA` <  "1940-01-01");

Parse error: syntax error, unexpected '`'

ok, lets remove '`' from table

$sql = mysql_query(SELECT * FROM  table WHERE  `LYTIS` =  "V" AND  `GIMIMO_DATA` <  "1940-01-01");

 

Parse error: syntax error, unexpected T_STRING

Ok, next attempt

$sql = mysql_query(SELECT * FROM  'table' WHERE  `LYTIS` =  "V" AND  `GIMIMO_DATA` <  "1940-01-01");

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING

 

 

:|

What to do? :(

Tried it on different versions, like 1.7.7 and 5.5

I'm new to this stuff, sorry if question is too noobish ^^"" :(

Link to comment
https://linustechtips.com/topic/619514-php-mysql/
Share on other sites

Link to post
Share on other sites

3 minutes ago, fizzlesticks said:

What is the output from mysql_error() with the original query?

Like this?

$sql = mysql_query('SELECT * 
FROM  `table` 
WHERE  `LYTIS` =  "V"
AND  `GIMIMO_DATA` <  "1940-01-01"');

I think it just skips it..

 

No database selected
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given

 

Boolean given it mens here:

$data = mysql_num_rows($sql); 

 

Link to comment
https://linustechtips.com/topic/619514-php-mysql/#findComment-7996052
Share on other sites

Link to post
Share on other sites

2 minutes ago, fizzlesticks said:

It's telling you "No database selected" which probably means your call to mysql_select_db() is failing. Add another echo mysql_error() after selecting your DB to see why it isn't working. 

Uhh thanks for suggesting that!!

 

 Access denied for user 'people'@'localhost' to database 'people'

 

I'll check all permissions tomorrow.

Thanks a lot! :D ^^""

Link to comment
https://linustechtips.com/topic/619514-php-mysql/#findComment-7996103
Share on other sites

Link to post
Share on other sites

First, you must not use the mysql extension in PHP - it is insecure and has been removed in PHP7. Instead, you should use mysqli, which is very similar, but safe to use and still supported.

 

Next, you do need the quotes around it - SQL statements are just strings passed into the function, and have no special status in PHP. That is why you are getting all the subsequent errors. PHP doesn't interpret it as a comment, it interprets it as a string, which is correct. As has been discussed above, the issue seems to be with your DB permissions, or how you are selecting the DB, and not a fault with the query itself.

HTTP/2 203

Link to comment
https://linustechtips.com/topic/619514-php-mysql/#findComment-7996122
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×