6 Replies - 638 Views - Last Post: 17 May 2012 - 10:06 PM Rate Topic: -----

#1 psir11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 16-May 12

PHP doesnt read/write MySQL

Posted 16 May 2012 - 03:46 PM

Hello,

I am working on a script, and for whatever reason PHP cant write/read MySQL. I know that the database is connected because I had it print if it connected successfully and it does. I also selected the right databse. I tried running the quarry that I am using in phpMyAdmin and it works just fine there. It doesnt give me any errors but instead just doesnt have any results.

Here is the code from my login.php file (I removed the login boxes to make sure the problem wasnt in the $_POST):

login.php (quick login test)
<?php 

//Include the includes/Connect to DB

define('158', TRUE ); 

require_once('header.inc.php'); 

//Login variables 

$username = 'test'; 
$password = 'test';

//$login =sprintf("SELECT * FROM Users WHERE username = '%s' ",
//mysql_real_escape_string($username)); 

$login = sprintf("SELECT * FROM Users WHERE username = '%s' AND password = '%s'", 
mysql_real_escape_string($username),
mysql_real_escape_string($password)); 

$SQLlogin = mysql_query($login); 


$userdata = mysql_fetch_array($SQLlogin);

echo $userdata['id']; 

?> 


Header requires dbConnect.php which connects to MySQL, so here is my dbConnect.php (and this works just fine I think):

<?php 

//prevent direct access

if(!defined('158')){die('Access Denied');}

//database options

$serverData = array(

'server' => 'localhost', 
'sqluser' => '*******',
'sqlpass' => '*******'
) ;

//connect to database 

$sqlConnect = mysql_connect($serverData['server'], $serverData['sqluser'], $serverData['sqlpass']); 

if (!$sqlConnect) { 
	die('Could Not Connect');
	}
	else {
	echo 'connection successful';} 
	
mysql_select_db('********_UPortal', $sqlConnect);

?>




Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: PHP doesnt read/write MySQL

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3730
  • View blog
  • Posts: 6,017
  • Joined: 08-June 10

Re: PHP doesnt read/write MySQL

Posted 16 May 2012 - 03:50 PM

Hey.

You should start by checking the mysql_query call return value to see if there was an error. (PHP doesn't print MySQL errors. You need to code that yourself.)
$SQLlogin = mysql_query($login);
if (!$SQLlogin) {
    trigger_error(mysql_error(), E_USER_ERROR);
}


That should tell you if the query is failing, and why.
Was This Post Helpful? 0
  • +
  • -

#3 psir11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 16-May 12

Re: PHP doesnt read/write MySQL

Posted 16 May 2012 - 03:55 PM

oh, I already tried that. I just removed the code for it already because it returned with no results.
Was This Post Helpful? 0
  • +
  • -

#4 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3730
  • View blog
  • Posts: 6,017
  • Joined: 08-June 10

Re: PHP doesnt read/write MySQL

Posted 16 May 2012 - 04:06 PM

OK. You really shouldn't remove it. Error checking should be a part of the database code, not just temporarily while developing. (Things do go wrong on production servers sometimes, and it's better to plan for it.)

In any case, if the query is successful, what does var_dump($userdata); print?
Was This Post Helpful? 0
  • +
  • -

#5 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3572
  • View blog
  • Posts: 10,413
  • Joined: 08-June 10

Re: PHP doesnt read/write MySQL

Posted 17 May 2012 - 12:04 AM

note: constants’ names cannot start with a number:

The PHP Manual said:

The name of a constant follows the same rules as any label in PHP. A valid constant name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thusly: [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*



additionally, you only use your query template (line #17) in your query without putting in the values (lines #18-#19 do absolutely nothing) (see below)

This post has been edited by Dormilich: 17 May 2012 - 10:07 PM

Was This Post Helpful? 0
  • +
  • -

#6 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3730
  • View blog
  • Posts: 6,017
  • Joined: 08-June 10

Re: PHP doesnt read/write MySQL

Posted 17 May 2012 - 07:17 AM

View PostDormilich, on 17 May 2012 - 07:04 AM, said:

additionally, you only use your query template (line #17) in your query without putting in the values (lines #18-#19 do absolutely nothing)

I thought the same thing at first glance, but it's actually just because the indentation is off. If you look again you'll see that lines #18 and #19 are parameters to the sprintf call on line #17.
Was This Post Helpful? 0
  • +
  • -

#7 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3572
  • View blog
  • Posts: 10,413
  • Joined: 08-June 10

Re: PHP doesnt read/write MySQL

Posted 17 May 2012 - 10:06 PM

damn it, you see how important indentation is.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1