-1

I'm having a MySQL/MySQLi/PHP Error with my link shortener the error is:

Fatal error: Call to a member function bind_param() on a non-object in
/home/exabit/public_html/9ui/index.php on line 50

here is the problematic line.

$reslove->bind_param("ss",$link, $short_url);

here is the rest of the code

<?php
$data_base = new mysqli ("http://host38.qnop.net/~exab","exab_ml","MKnOz3A]h~aw","exab_ml");
function generateRandomString($length = 3) {
$key = 'abcdefghijklmnopqrstuvwxyz1234567890';
$keyLength = strlen($key);
$string = '';
for ($i = 0; $i < $length; $i++) {
$string .= $key[rand(0, $keyLength - 1)];
}
return $string;
}
if (isset($_GET['title'])) {
$reslove = $data_base->prepare("SELECT * FROM links WHERE title=?");
$reslove->bind_param("s", $_GET['title']);
$reslove->execute();
$goto = $reslove->get_result()->fetch_array();
$goto1 = $goto[1];
header("Location: $goto1");
}
if (isset($_POST['submit'])) {
$short_url = generateRandomString();
if (!preg_match("/^(http|https):/", $_POST['long_url'])) {
$_POST['long_url'] = 'http://'.$_POST['long_url'];
}
$link = $_POST['long_url'];
$reslove = $data_base->prepare("INSERT INTO links VALUES('',?,?)");
$reslove->bind_param("ss",$link, $short_url);
$reslove->execute();
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<center>
<form>
<p style="color:#05ff19;font-family:Tahoma;font-size:16px;text-align:center">Shortened Link: </p><input id=shortenedurl style="background-color:#000;color:#05ff19;font-family:Tahoma;font-size:16px;vertical-align:middle;border:1px solid #05ff19" type="text" value=<?php echo "9ui.co/$short_url";}?>'>
</form>
</center>

The misspelling of resolve has nothing to do with it.
Thanks in advance,
Zack Davies

Ehsan Khodarahmi
  • 4,772
  • 10
  • 60
  • 87
Z Davies
  • 17
  • 5

2 Answers2

2

The error "Call to a member function bind_param() on a non-object" implies that $reslove is not an object, therefore you cannot call a member function (aka a method) on something that is not an object. How can $resolve not be an object? Well, the ->prepare method call on line 49 will return false if an error occurs preparing the SQL statement and, of course, false is a "non-object" so that'll cause the error message you got.

Looking at line 49, where $resolve gets set, hmmmm that SQL statement looks funny. The syntax for an INSERT command is normally ...

INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);

You have left out the (column1,column2,column3,...) which explicitly states the column names and their order. Leaving this out, if it ever does work, is risky and/or fragile because you'd be assuming the columns match your values, which may not always be the case. Much safer to explicitly state the (column1,column2,column3,...).

BareNakedCoder
  • 3,257
  • 2
  • 13
  • 16
  • So is it because the columns are missing? – Z Davies Dec 26 '15 at 18:00
  • Yes, I believe so. Some databases allow the really bad practice of not explicitly listing the column names. Not sure about MySQL but either it or maybe "prepare" is more strict in preventing you from doing this really bad practice. Try explicitly listing the column names and see if it works. – BareNakedCoder Dec 26 '15 at 18:03
  • Also, your "SELECT * FROM ..." with "*" is also a bad practice. It results in fragile code. Sometime in the future you (or someone else) may recreate the table with a CREATE TABLE command that lists the columns in a slightly different order. That would mysteriously cause broken code to surface all over your app, wherever you implicitly relied upon the order (and number) of columns. – BareNakedCoder Dec 26 '15 at 18:06
  • Okay so would I put `INSERT * INTO links WHERE 1 VALUES` ? – Z Davies Dec 26 '15 at 18:06
  • What would I put instead of `*` (Sorry I'm new with this) – Z Davies Dec 26 '15 at 18:07
  • No, what are the names of the columns in your table? Assuming they are called A,B,C, your statement would be `INSERT INTO links (A,B,C) VALUES ('', ?, ?)` – BareNakedCoder Dec 26 '15 at 18:08
  • And assuming you want to retrieve columns A,B,C from the table you'd say `SELECT A,B,C FROM links WHERE title = ?` – BareNakedCoder Dec 26 '15 at 18:10
  • Umm.. I'm not sure.. Could I PM you a screen shot? – Z Davies Dec 26 '15 at 18:10
  • Okay.. How do I PM... :( – Z Davies Dec 26 '15 at 18:12
  • LOL!, funny you should ask. I was hoping you knew. I'm relatively new on SO. I don't know how to PM yet. Let me look around a bit. – BareNakedCoder Dec 26 '15 at 18:13
  • Not quite there. You've selected the "Structure" tab in phpMyAdmin to list the tables (just 1). Now click the word "structure" in the row with "links" table to see the list of columns names for your table. Repost that screen capture. – BareNakedCoder Dec 26 '15 at 18:18
  • Okay, so first about your error. Because you didn't explicitly list the column name and order, it would attempt to assign the first value, `''`, to the first column in your table, `id`, which is an integer column (with auto-increment), not a string. Since you want the `id` field to autoincrement on its own, you REALLY do need to explicitly state the column names. See my next comment ... – BareNakedCoder Dec 26 '15 at 18:28
  • So how would I fix this? – Z Davies Dec 26 '15 at 18:31
  • Your table has columns id, url, title, and created. You don't want to specify either id (let it autoincrement itself) or created (it will set itself to current date/time itself). I'm trying to figure out what should go in url and in title columns ... What should go in title? $short_url is just a random string, neither a title or url, so not sure why you're inserting that. – BareNakedCoder Dec 26 '15 at 18:31
  • You should have `$reslove = $data_base->prepare("INSERT INTO links (url, title) VALUES(?,?)");` and then `$reslove->bind_param("ss", $short_url, $link);` assuming your want $short_url as the url and $link as the title. (oops, fixed typo uri-->url) – BareNakedCoder Dec 26 '15 at 18:36
0

Just change this

$reslove->bind_param("ss",$link, $short_url);

TO

$reslove->bind_param($link, $short_url);

update

add the column names in your prepare statement and then give them a value like this

$reslove = $data_base->prepare("INSERT INTO links (id, link, short_url) VALUES(?,?,?)");
$reslove->bind_param('',$link, $short_url);
Alaa M. Jaddou
  • 1,180
  • 1
  • 11
  • 30