tee1
Posts: 1
Joined: Thu Dec 03, 2015 9:03 am

sql queries in php

Sat Jan 09, 2016 6:17 pm

Hie everyone.
l am working on a project that involves connecting a php application and MySQL database. Connection to the database is made successfully but the problem is that all queries to the database using php are failing. l did install the php5-mysql package. The code below shows a query to add a record to the table 'users':
<?php
$dbUser="root";
$dbPass="<mypassword>";
$dbDatabase="<databasename>";
$dbHost="localhost";
$dbConn=mysql_connect($dbHost,$dbUser,$dbPass);
if(dbConn){
mysql_select_db($dbDatabase);
print("Successful connection");
mysql_query(INSERT INTO 'users' ('username','password') VALUES('<myusername>','<mypassword>'));
}else{
print("Connection failed");
}
?>

When l go to the 'users' table nothing will have been added but on the webpage it print Successful connection. Your help will be really appreciated

User avatar
DougieLawson
Posts: 40214
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: sql queries in php

Sun Jan 10, 2016 12:52 am

Test the results from both your connection and your database select.

Code: Select all

<?php
 $dbconn = mysql_connect('localhost', 'someuseridhere', 'somesupersecretpasswordhere' );
 if (!$dbconn) {
        die('DB001T DB Connection Error: ' . mysql_error());
 }
 $dbsel = mysql_select_db('LOG_DB', $dbconn);
 if (!$dbsel) {
        die('DB002T Can\'t use database LOG_DB: ' . mysql_error());
 }?>
And again when you run your SQL

Code: Select all

$rrows = mysql_query($isrtrow) or die("DB003E ".mysql_error());
Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All fake doctors are on my foes list.

Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

User avatar
aelsas
Posts: 64
Joined: Fri Jul 06, 2012 11:23 pm
Location: Frankfurt, Germany
Contact: Website

Re: sql queries in php

Sun Jan 10, 2016 2:27 pm

Is it really 'users'? Normally table names without ' ' in SQL!

User avatar
rpdom
Posts: 17723
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: sql queries in php

Sun Jan 10, 2016 8:58 pm

aelsas wrote:Is it really 'users'? Normally table names without ' ' in SQL!
Actually, normally with backticks to identify a table, row or column identifier. The single quotes would indicated a literal value. Oh, and the whole query should be in double quotes too.

Code: Select all

mysql_query("INSERT INTO `users` (`username`,`password`) VALUES('<myusername>','<mypassword>')");

User avatar
Jim Manley
Posts: 1600
Joined: Thu Feb 23, 2012 8:41 pm
Location: SillyCon Valley, California, and Powell, Wyoming, USA, plus The Universe
Contact: Website

Re: sql queries in php

Mon Jan 11, 2016 3:56 am

rpdom wrote:
aelsas wrote:Is it really 'users'? Normally table names without ' ' in SQL!
Actually, normally with backticks to identify a table, row or column identifier. The single quotes would indicated a literal value. Oh, and the whole query should be in double quotes too.

Code: Select all

mysql_query("INSERT INTO `users` (`username`,`password`) VALUES('<myusername>','<mypassword>')");
Backticks are optional because they're meant to allow creation of column names and other identifiers that contain spaces and reserved words and characters, although reserved elements should absolutely be avoided for obvious reasons.
The best things in life aren't things ... but, a Pi comes pretty darned close! :D
"Education is not the filling of a pail, but the lighting of a fire." -- W.B. Yeats
In theory, theory & practice are the same - in practice, they aren't!!!

Return to “General discussion”