我正在尝试从数据库中回显数据,但是,即使所有3个变量都存在,我也会收到以下错误。
注意:未定义的变量:第58行的/Applications/MAMP/htdocs/PhpProject2/testing.php中的fran_phone
对于以下内容:
代码
mysqli_report(MYSQLI_REPORT_INDEX);
$dbc = new mysqli("localhost", "root", "root", "One_Delivery");
$dbc->set_charset("utf8mb4");
if (isset($_GET['area'])) {
$franc_details = $_GET['area'];
$get_franc_dets = "SELECT * FROM Franc_dets WHERE Fran_City = '$franc_details'";
$run_get_franc_dets = mysqli_query($dbc, $get_franc_dets);
mysqli_stmt_execute($run_get_franc_dets);
while ($row_get_franc_dets = mysqli_fetch_array($run_get_franc_dets)) {
$franc_phone = $row_get_franc_dets['Fran_Contact_Num'];
$twit = $row_get_franc_dets['Twitter'];
$fb = $row_get_franc_dets['Fb'];
}
}
?>
<div id='franc_div' >
<table id='franchise_dets'>
<tr id='frnc_tbl'>
<td class='collapse'>
<img src='./Images/franc_dets_phone.png' height='50' width='50' alt='Call us'>
</td>
<td class='phn_dets'>
<p id='phn_title'>Problems ordering?</p>
<p id='phn_numb'><?php echo $franc_phone ?></p>
</td>
<td class='collapse'>
<img src='./Images/franc_dets_twitter.png' height='50' width='50' alt='Twitter logo'>
</td>
<td class='twitter_dets'>
<p id='sm_title'>Social media</p>
<a id='sm_twit' href='https://twitter.com/<?php echo $twit ?>'>@<?php echo $twit ?></a>
</td>
<td class='collapse'>
<img src='./Images/franc_dets_fb.png' height='50' width='50' alt='Facebook logo' >
</td>
<td class='fb_dets'>
<a id='sm_fb' href='https://www.facebook.com/<?php echo $fb; ?>'><?php echo $fb; ?></a>
</td>
</tr>
</table>
</div>
我哪里出错了?我该怎么做才能解决它
答案 0 :(得分:1)
所以有两件事是错的。
您显然正在尝试学习/切换到准备好的语句。
mysqli_stmt_execute
需要声明,但您要为其提供mysqli_result
个对象。
您需要使用mysqli_query
。
mysqli_prepare
$sql = "SELECT * FROM Franc_dets WHERE Fran_City = ?;";
if ($stmt = mysqli_prepare($dbc, $sql)) {
mysqli_stmt_bind_param($stmt, "s", $franc_details);
mysqli_stmt_execute($stmt);
// Check how many if any rows were returned.
$num_rows = mysqli_stmt_num_rows($stmt);
// Do what you were already doing,
// and loop through each returned row.
}
请注意,如果返回0行,那么您将得到相同的错误。因为那时不会定义变量。