Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single Value Mysqli [duplicate]

Tags:

php

mysqli

I am trying to write a function that will check for a single value in the db using mysqli without having to place it in an array. What else can I do besides what I am already doing here?

function getval($query){     $mysqli = new mysqli();     $mysqli->connect(HOST, USER, PASS, DB);     $result = $mysqli->query($query);     $value = $mysqli->fetch_array;     $mysqli->close();     return $value; } 
like image 664
Refiking Avatar asked Jul 12 '12 16:07

Refiking


2 Answers

How about

$name = $mysqli->query("SELECT name FROM contacts WHERE id = 5")->fetch_object()->name;  
like image 189
Mike Avatar answered Sep 22 '22 12:09

Mike


The mysql extension could do this using mysql_result, but mysqli has no equivalent function as of today, afaik. It always returns an array.

If I didn't just create the record, I do it this way:

$getID = mysqli_fetch_assoc(mysqli_query($link, "SELECT userID FROM users WHERE something = 'unique'")); $userID = $getID['userID']; 

Or if I did just create the record and the userID column is AI, I do:

$userID = mysqli_insert_id($link); 
like image 28
prl77 Avatar answered Sep 23 '22 12:09

prl77