helmet648 helmet648 - 4 months ago 5x
PHP Question

mysql row id is returning 0 when it should be returning a higher number

thanks for taking the time to read this and i apologize in advance for how messed up my coding is, i'm trying to teach myself. i am running into an error when it comes to getting the id of the row when the check is run if the name, email or telephone of the input matches the db. i'm trying to get the id so that i can tie that id as the "owner_id" of the message when i store it.

Fatal error: Call to undefined method mysqli_stmt::fetch_assoc() in /var/www/a-website.com/html/inc/db3.php on line 35

line 35 is

while($row = $prepared_check->fetch_assoc()){

here is the whole php file. its required_once in another php file, and run in that php file using

checkRecords($name, $tel2, $email);

anyways, onto the php file.

$run_check = 1;

function newInsert($name, $email, $tel, $mysqli){
$prepared_insert = $mysqli->prepare("INSERT INTO call_list (name, email, tel) VALUES (?, ?, ?)");
$prepared_insert->bind_param('sss', $name, $email, $tel);
$run_check = 1;

function storeMessage($id, $message, $mysqli){
$prepared_message = $mysqli->prepare("INSERT INTO messages (owner_id, message) VALUES (?, ?)");
$prepared_message->bind_param('ss', $id, $message );
$run_check = 0;

function checkRecords($name, $email, $tel, $message){
$servername = "localhost";
$username = "a_valid_user";
$password = "some_valid_password";
$dbname = "call_list";
$mysqli = new mysqli($servername, $username, $password, $dbname);
$_SESSION['result_sql'] = "<div class='alert alert-danger'>Failed to connect to MySQL:(".$mysqli->errno.")". $mysqli->error."</div>";
$prepared_check = $mysqli->prepare("SELECT * FROM call_list WHERE name = ? OR tel = ? OR email = ?");
$prepared_check->bind_param('sss', $name, $tel, $email);
$nm_rows = $prepared_check->num_rows;
$prepared_check->bind_result($id, $name, $tel, $email);
while($row = $prepared_check->fetch_assoc()){
echo "grabbed ". $nm_rows ." results<br><br>";

if($nm_rows === 0){
echo "trying to insert<br><br>". $name ."<br><br>". $email ."<br><br>". $tel;
newInsert($name, $email, $tel2, $mysqli);
echo "i'm trying to store a message with the owner id of ". $row['id'] ." who should be".$row['name'];
echo "<br><br> here's the message: <br><br>". $message ."<br><br><br>";
storeMessage($id, $message, $mysqli);



A mysqli statement don't have fetch_assoc() method. Instead, you may just use: fetch()