A simple PHP query..

manuraj.dhanda

New Member
Messages
21
Reaction score
0
Points
1
Hii Guyz..

Am new to PHP world. I want to use PHP on my server side to talk to db and return the results.

So, instead of taking a tutorial for PHP, am straight in here. Although I have read that w3schools tutorial.

I want to do a select query with placeholders as follows:

public function getLoginInfo(InfoVO $vo)
{
$_email = $vo->email;;
$_info = $vo->info;
$link = mysql_connect("localhost", "xxxx", "yyyyy");
mysql_select_db('mydb', $link);
$result = mysql_query("SELECT * FROM tbl_info where email='%s' and info='%s'",array($_email,$_info)) or die("Invalid query: " . mysql_error());
mysql_close($link);
return $result;
}

Can someone here help me to correct this query??

Thanks.
 

TechAsh

Retired
Messages
5,853
Reaction score
7
Points
38
PHP:
public function getLoginInfo(InfoVO $vo)
{
$_email = $vo->email;
$_info = $vo->info;
$link = mysql_connect("localhost", "xxxx", "yyyyy");
mysql_select_db('mydb', $link);
$result = mysql_query("SELECT * FROM tbl_info where email='%s' and info='%s'",array($_email,$_info)) or die("Invalid query: " . mysql_error());
mysql_close($link);
return $result;
}

First, do you get any errors when using this, or does it just not work?
Second there are two ";" after email in line 3. Does that fix it?
 
Last edited:

manuraj.dhanda

New Member
Messages
21
Reaction score
0
Points
1
IT simply doesn't work.
I am using Servicecapture to get the request and response.

Here is the response:

<b>Warning</b>: mysql_query(): supplied argument is not a valid MySQL-Link resource in <b>F:\xampp\htdocs\weborb\Services\com\live\flats\LoginInfoDelegate.php</b> on line <b>14</b><br />
Invalid query:

Line 14 is:

PHP:
$result = mysql_query("SELECT * FROM tbl_info where email='%s' and info='%s' ",array($_email,$_info)) or die("Invalid query: " . mysql_error());
 

TechAsh

Retired
Messages
5,853
Reaction score
7
Points
38
I'm sorry but as PHP/MySQL is not my strong point I can't help any more. The only thing I can suggest is carefully checking that your query is valid (Maybe read a MySQL manual.)

Another member, who is good at PHP, should be along soon to help.
 

phpasks

New Member
Messages
145
Reaction score
0
Points
0
Hii Guyz..

Am new to PHP world. I want to use PHP on my server side to talk to db and return the results.

So, instead of taking a tutorial for PHP, am straight in here. Although I have read that w3schools tutorial.

I want to do a select query with placeholders as follows:

public function getLoginInfo(InfoVO $vo)
{
$_email = $vo->email;;
$_info = $vo->info;
$link = mysql_connect("localhost", "xxxx", "yyyyy");
mysql_select_db('mydb', $link);
$result = mysql_query("SELECT * FROM tbl_info where email='%s' and info='%s'",array($_email,$_info)) or die("Invalid query: " . mysql_error());
mysql_close($link);
return $result;
}

Can someone here help me to correct this query??

Thanks.
PHP:
  <?php
// This could be supplied by a user, for example
$firstname = 'fred';
$lastname  = 'fox';

// Formulate Query
// This is the best way to perform a SQL query
// For more examples, see mysql_real_escape_string()
$query = sprintf("SELECT firstname, lastname, address, age FROM friends WHERE firstname='%s' AND lastname='%s'",
    mysql_real_escape_string($firstname),
    mysql_real_escape_string($lastname));

// Perform Query
$result = mysql_query($query);

// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $query;
    die($message);
}

// Use result
// Attempting to print $result won't allow access to information in the resource
// One of the mysql result functions must be used
// See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc.
while ($row = mysql_fetch_assoc($result)) {
    echo $row['firstname'];
    echo $row['lastname'];
    echo $row['address'];
    echo $row['age'];
}

// Free the resources associated with the result set
// This is done automatically at the end of the script
mysql_free_result($result);
?>

Try it. Then tell me
 

gamerdude

New Member
Messages
101
Reaction score
0
Points
0
Try this

PHP:
<?php
$con = mysql_connect("localhost","root","****");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("$db_name", $con);

$result = mysql_query("SELECT * FROM tbl_info
WHERE $C1=$Q1r, $C2=$Q2");

// This will print the data out line by line

while($row = mysql_fetch_array($result))
  {
  echo $row[$C1] . " " . $row[$C2];
  echo "<br />";
  }

?>
 
Last edited:

VPmase

New Member
Messages
914
Reaction score
1
Points
0
Have you tried any other mysql queries? If so, do they work?
 

gamerdude

New Member
Messages
101
Reaction score
0
Points
0
Please note that phpasks's example was the only secure one. All the other ones may have been simpler but are also unsecure.
 

manuraj.dhanda

New Member
Messages
21
Reaction score
0
Points
1
Also, I have a php class UserVO.php as follows:
PHP:
<?php
class UserVO
{
    var $userId;
    var $pwd;
}
?>
And from my query, I want to fetch the results in the form of array of UserVO objects, something like below:
PHP:
....
......
$query = sprintf("SELECT userId, pwd FROM table1 WHERE userId='%s' AND pwd ='%s'",
    mysql_real_escape_string($userId),
    mysql_real_escape_string($pwd));

// Perform Query
$result = mysql_query($query); // Here I want this $result as an array of UserVO objects. How can I do it in PHP??
Thanks,
Manu.
 

sybregunne

Member
Messages
54
Reaction score
0
Points
6
Just a thought... I read somewhere that mysql functions have many problems and issues. why not use the mysqli functions instead.

it is basically the same commands and syntaxes than mysql.
Edit:
PHP:
public function getLoginInfo(InfoVO $vo) {
  $_email = $vo->email;           
  //make sure that this variable is in username@domain.ext
  //meaning no %20 and the like ...
  $_info = $vo->info;
  $link = mysqli_connect("localhost", "xxxx", "yyyyy","mydb");
  // Check your query string. sometimes it is simpler to do
  //  $query="SELECT * FROM tbl_info where email='%s' and info='%s'",array($_email,$_info);
  $query="SELECT * 
              FROM tbl_info 
              WHERE email='".$_email."' and info='".$_info."'";
  $result = mysqli_query($link,$query) or die("Invalid query: " . mysql_error());
  //I am assuming you are expecting only one row. right?
  $row=mysqli_fetch_array($result);
  mysqli_free_result($result);
  mysqli_close($link);
  return $row;
}

It is always a good practice to free your results, to conserve your system resources. Then I suggest to use a global $link, and just close your database link as the program is about to end.

Hope this helps...
 
Last edited:
Top