Error in code

Hello everyone,

I’m a newbie in PHP thus have an issue running the following code. it always says
(Deprecated: mysql_connect(): : The mysql extension is deprecated and will be removed
in the future: use mysqli or PDO instead in C:\wamp\www\showDetails1.php
on line 2) or see attached image.
I changed the function to mysqli_connect(), infortunately it generates more errors
in this code I try to inquiry data from a table using a dropdow list.

<?php
$link = mysql_connect("localhost","root","");
mysql_select_db("home_work",$link);
$sql = "SELECT * FROM dropdown ";
$aResult = mysql_query($sql);
if($_REQUEST['frm_action'] == 3)
{
if ($_REQUEST['cust_id'] == 0)
{
$id = $_REQUEST['cust_id'];
$sqlCustomer = "SELECT * FROM dropdown ";
}
else
{
$id = $_REQUEST['cust_id'];
$sqlCustomer = "SELECT * FROM dropdown WHERE id ='$id'";
}
$aCustomer = mysql_query($sqlCustomer);
}
?>
<html>
<head>
<script type="text/javascript">
function changeSID()
{
oForm       = eval(document.getElementById("frmForm"));
iCustomerId = document.getElementById("sid").value;
url         = "showDetails1.php?frm_action=3&cust_id=" +iCustomerId;
document.location = url;
}
</script>
</head>

<body>
<form name="frmForm" id="frmForm" >
<table border="0" cellspacing="2" cellpadding="2" width="40%">
<tr>
<td align="right" ><strong>Sid</strong></td>
<td align="left"><select name="sid" id="sid" onchange="javascript:changeSID();">
<option value="">Select</option>
<option value="0">All</option> 

<?php
$sid1 = $_REQUEST['cust_id'];

while($rows=mysql_fetch_array($aResult,MYSQL_ASSOC))
{
$id  = $rows['id'];
$sid = $rows['sid'];
if($sid1 == $id)
{
$chkselect = 'selected';

}
else
{
$chkselect ='';
}
?>
<option value="<?php echo  $id;?>"<?php echo $chkselect;?>><?php echo $sid;?></option>
<?php } ?>
</td>
</tr>
<?php if($_REQUEST['frm_action'] == 3) { ?>
<tr>
<td colspan="2">
<table  style="border:1px solid #003366;" cellspacing="2" cellpadding="2" width="100%" bgcolor="#003366">
<tr bgcolor="#EFEFEF">
<td><b><font color='Red'>Sid</font></b></td>
<td><b><font color='Red'>Sname</font></b></td>
<td><b><font color='Red'>Age</font></b></td>
</tr>
<?php
while($row1 = @mysql_fetch_array($aCustomer,MYSQL_ASSOC))
{
$sid   = $row1['sid'];
$sname = $row1['sname'];
$age   = $row1['age'];
?>
<tr bgcolor="#FFFFFF">
<td><b><font color='#663300'><?php echo $sid;?></font></b></td>
<td><b><font color='#663300'><?php echo $sname;?></font></b></td>
<td><b><font color='#663300'><?php echo $age;?></font></b></td>
</tr>
<?php } ?>
</table>
</td>
</tr>
<?php } ?>
</table>
</form>
</body>
</html>

Is there anyone of help? any help will be appreciated.

Welcome to the forum. This is the place to get help and answers and some guidance as you learn and improve your skills.

Firstly, the message indicated mysql_connect is deprecated and will be removed. It still works. This is really a ‘warning’ and not an error.
Secondly, it is pointing you to a class, mysqli which must be implemented in a slightly different way. Reading the documentation (and associated comments) should guide you.

I have not written PHP code in many, many years so will not attempt to guide you for fear of giving you a bad example. But I have no doubt there will quickly follow another reply with some sample code to get you going.

If you’re switching, it might be worth going to PDO which would support databases other than MySQL, should you need them at some point. There’s a recent article here about PDO: http://www.sitepoint.com/re-introducing-pdo-the-right-way-to-access-databases-in-php/

1 Like

What’s the error you get when you use mysqli_connect()?
I also highly recommend you use PDO, as droopsnoot suggested. The advantages are too great not to.

If you have no choice but to use mysqli_connect(), then use mysqli_connect_errno() and mysqli_connect_error() to debug the issue.

Thanks a lot everyone, I really appreciate your response.
However, I tried to fix the problem as you advised. Unfortunately, occurred another one regarding mysql_fetch_array ( ) function
(Warning: mysql_fetch_array() expects parameter 1 to be resource, string given in C:\wamp\www\showDetails1.php on line 46).
here is the improve code:

<?php
$link = mysqli_connect("localhost","root","", "home_work");
//mysql_select_db("home_work",$link);
$sql = "SELECT * FROM dropdown ";
$aResult = mysqli_query($link, $sql);
if($_REQUEST['frm_action'] == 3)
{
if ($_REQUEST['cust_id'] == 0)
{
$id = $_REQUEST['cust_id'];
$sqlCustomer = "SELECT * FROM dropdown ";
}
else
{
$id = $_REQUEST['cust_id'];
$sqlCustomer = "SELECT * FROM dropdown WHERE id ='$id'";
}
$aCustomer = mysqli_query($link, $sqlCustomer);
}
?>
<html>
<head>
<script type="text/javascript">
function changeSID()
{
oForm       = eval(document.getElementById("frmForm"));
iCustomerId = document.getElementById("sid").value;
url         = "showDetails1.php?frm_action=3&cust_id=" +iCustomerId;
document.location = url;
}
</script>
</head>

<body>
<form name="frmForm" id="frmForm" >
<table border="0" cellspacing="2" cellpadding="2" width="40%">
<tr>
<td align="right" ><strong>Sid</strong></td>
<td align="left"><select name="sid" id="sid" onchange="javascript:changeSID();">
<option value="">Select</option>
<option value="0">All</option> 

<?php
$sid1 = $_REQUEST['cust_id'];

while($rows=mysql_fetch_array($sqlCustomer, MYSQL_ASSOC))
{
$id  = $rows['id'];
$sid = $rows['sid'];
if($sid1 == $id)
{
$chkselect = 'selected';

}
else
{
$chkselect ='';
}
?>
<option value="<?php echo  $id;?>"<?php echo $chkselect;?>><?php echo $sid;?></option>
<?php } ?>
</td>
</tr>
<?php if($_REQUEST['frm_action'] == 3) { ?>
<tr>
<td colspan="2">
<table  style="border:1px solid #003366;" cellspacing="2" cellpadding="2" width="100%" bgcolor="#003366">
<tr bgcolor="#EFEFEF">
<td><b><font color='Red'>Sid</font></b></td>
<td><b><font color='Red'>Sname</font></b></td>
<td><b><font color='Red'>Age</font></b></td>
</tr>
<?php
while($row1 = @mysql_fetch_array($aCustomer,MYSQL_ASSOC))
{
$sid   = $row1['sid'];
$sname = $row1['sname'];
$age   = $row1['age'];
?>
<tr bgcolor="#FFFFFF">
<td><b><font color='#663300'><?php echo $sid;?></font></b></td>
<td><b><font color='#663300'><?php echo $sname;?></font></b></td>
<td><b><font color='#663300'><?php echo $age;?></font></b></td>
</tr>
<?php } ?>
</table>
</td>
</tr>
<?php } ?>
</table>
</form>
</body>
</html>

If you switch to mysqli, you can’t use old mysql* functions (including mysql_fetch_array) any more, you have to switch completely. So you’d need to use mysqli_fetch_assoc() to get your data. Some sample code here under the “easy migration” section: http://php.net/manual/en/mysqli.quickstart.dual-interface.php

Also you need to be using $aCustomer in there - $sqlCustomer is your original query, not your result set.

Thanks guys, your input was really helpful.
I did it.
cheers,

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.