0

I am trying to echo data out of a database, however, i am getting the following errors, even though all 3 variables have been.

Notice: Undefined variable: fran_phone in /Applications/MAMP/htdocs/PhpProject2/testing.php on line 58

For the following:

  • Fran_phone
  • Twit
  • Fb

Code

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>

where did i go wrong? what can i do to resolve it

10
  • I don't see a fran_phone in your code. I see $franc_phone, but fran!=franc Commented May 8, 2016 at 0:21
  • @Sean sorry you lost me. yes $franc_phone Commented May 8, 2016 at 0:59
  • 1
    You are only declaring those variables if (isset($_GET['area'])) { so the first time into the page when the user has not yet entered anything they will of course be UNDEFINED Commented May 8, 2016 at 1:11
  • @RiggsFolly i should have explained better, the url is .... area=Enfield Commented May 8, 2016 at 2:03
  • @Monroe yes, but if no rows are returned then the variables still aren't declared. Did you even care reading my answer which actually mentioned that? Commented May 8, 2016 at 5:44

1 Answer 1

1

So there's 2 things wrong.

You're clearly trying to learn/switch to prepared statements. mysqli_stmt_execute requires a statement, but you're giving it an mysqli_result object.

Instead of using mysqli_query you need to use 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.
}

Note that if 0 rows are returned, then you would get the same error(s). As then the variables won't be defined.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.