Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- if (isset($_GET['username']))
- {
- $username=$_GET['username'];
- if ($username === '')
- {
- exit;
- }
- $host = 'localhost';
- $user = 'user';
- $pass = 'pass';
- $db = 'db';
- $table = 'forum_cookies';
- $mysqli = new mysqli($host,$user,$pass,$db);
- if ($mysqli->connect_errno)
- {
- printf("Connect failed: (%s) %s\n", $mysqli->errno, $mysqli->connect_error);
- exit;
- }
- //get one row example:
- if ($result = $mysqli->prepare("SELECT * FROM $table WHERE username=?"))
- {
- $result->bind_param('s', $username);
- $result->execute();
- $result->bind_result($ri_username, $ri_date, $ri_user_id, $ri_p_cookies);
- $result->fetch();
- //do stuff with $ri_username, $ri_date, $ri_user_id, $ri_p_cookies here
- $result->close();
- }
- else
- {
- printf("Query failed: (%s) %s\n", $mysqli->errno, $mysqli->error);
- exit;
- }
- //or:
- if ($result = $mysqli->prepare("SELECT * FROM $table WHERE username=?"))
- {
- $result->bind_param('s', $username);
- $result->execute();
- $result->bind_result($ri_username, $ri_date, $ri_user_id, $ri_p_cookies);
- $result->fetch();
- }
- else
- {
- printf("Query failed: (%s) %s\n", $mysqli->errno, $mysqli->error);
- exit;
- }
- //do stuff with $ri_username, $ri_date, $ri_user_id, $ri_p_cookies here
- $result->close();
- //get more than one row example:
- if ($result = $mysqli->prepare("SELECT * FROM $table WHERE sender=? OR receiver=?"))
- {
- $result->bind_param('ss', $username, $username);
- $result->execute();
- $result->bind_result($r_username, $r_target_username, $isupvote, $date);
- $no_results=true;
- while ($result->fetch())
- {
- //do stuff with $r_username, $r_target_username, $isupvote, $date
- //in the next while passtrough, a new row will be loaded into those variables
- //or if you want to keep them all, load them into an array (there's a better function for this though, that does it all in one go)
- $no_results=false;
- }
- $result->close();
- }
- else
- {
- printf("Query failed: (%s) %s\n", $mysqli->errno, $mysqli->error);
- exit;
- }
- //insert one row example
- //with additional error checks (which have been omited in the above SELECT queries)
- //you can include them if you want, but it's kinda superfluous, I just put them here as an example
- if ($result = $mysqli->prepare("INSERT INTO $table VALUES (?,?,?,?)"))
- {
- if (!$result->bind_param('ssss', $username, $target_username, $isupvote, $date)) {
- printf("Binding parameters failed: (%s) %s\n", $stmt->errno, $stmt->error);
- exit;
- }
- if (!$result->execute()) {
- printf("Execute failed: (%s) %s\n", $stmt->errno, $stmt->error);
- exit;
- }
- $result->close();
- }
- else
- {
- printf("Query failed: (%s) %s\n", $mysqli->errno, $mysqli->error);
- exit;
- }
- //then after the last query:
- $mysqli->close();
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement