Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

stored procedure, convert string to integer

Codes here !

Moderators: macek, egami, gesf

stored procedure, convert string to integer

Postby clement » Sun Oct 07, 2012 10:43 pm

Hi I am new to php.
I am trying to query by means of stored procedure.

Stored procedure "name_employee" is working.
I am not sure how to convert the variable "$sql" into number,

person.html
Code: Select all
<html>
<head>
<title>
   Ajax demo 2 using PHP and MySQL by Clement Yap
</title>
<script type="text/javascript">
function showUser(str)
{
   if (str=="")//If nothing, then do nothing.
     {
        document.getElementById("txtHint").innerHTML="";
        return;
     }

   if (window.XMLHttpRequest)
     {// code for new browsers IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
     }
   else
     {// code for old browsers IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
     }

   xmlhttp.onreadystatechange=function()
     {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
           document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
        }
     }

    xmlhttp.open("GET","getuser.php?q="+str,true);
   xmlhttp.send();
}


</script>
</head>
<body>

<form>
<select name="people" onchange="showUser(this.value)">
<option value="0">Select a person:</option>
<option value="1">Clement</option>
<option value="2">Ellen</option>
<option value="3">Joseph</option>
<option value="4">Louis</option>
</select>
</form>

<br />

<div id="txtHint"><b>Person info will be listed here.</b></div>

</body>
</html>

getuser.php
Code: Select all
<?php
$q=$_GET['q'];

$con = mysql_connect('localhost', 'root', '');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("employee", $con);

//$sql="SELECT * FROM people WHERE id = '".$q."'";//by direct mysql syntax

$sql="call name_employee("intval($q)")";//by stored procedure

$result = mysql_query($sql);

echo "<table border='1'>
<tr>
<th>FirstName</th>
<th>LastName</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['FirstName'] . "</td>";
  echo "<td>" . $row['LastName']  . "</td>";
  echo "<td>" . $row['Age']       . "</td>";
  echo "<td>" . $row['Hometown']  . "</td>";
  echo "<td>" . $row['Job']       . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);
?>


Please advise
Thanks
Clement
clement
New php-forum User
New php-forum User
 
Posts: 1
Joined: Sun Oct 07, 2012 10:21 pm

Re: stored procedure, convert string to integer

Postby seandisanti » Thu Oct 11, 2012 8:39 am

try changing

Code: Select all
$sql="call name_employee("intval($q)")";//by stored procedure


to

Code: Select all
$sql="call name_employee(".$q.")";//by stored procedure
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm


Return to mySQL & php coding

Who is online

Users browsing this forum: No registered users and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.