如果代码在数据库中,我需要它来生成一个随机数

时间:2015-12-12 21:44:30

标签: php pdo

require("inc/connection.php");
if ($link->connect_error)
    die('Connect Error: '.$link->connect_errno.': '.$link->connect_error);
$insertedcode = $_POST['code'];
$results = $link->query("SELECT code FROM code WHERE code = :code");
$query_params = array( 
    ':code' => $_POST['code']
);

$stmt = $link->prepare($results);
$result = $stmt->execute($query_params);
$row = $stmt->fetch();
if($row) {
    $number = mt_rand(0,3999);
    echo $number;
}

这就是我的想法,我认为我有随机数部分。但由于某种原因,它给了我这个错误:

  

致命错误:在非对象中调用成员函数execute()   第21行的C:\ wamp \ www \ RoLuck \ dashboard.php

它不会执行,我不知道为什么。

2 个答案:

答案 0 :(得分:0)

你在使用mysqli的代码上执行的顺序错误,对吧?如果您在没有分配给代码的实际参数的情况下进行查询,那就是原因。在查询之前准备查询,而不是之后。

public class MainActivity extends AppCompatActivity {
    private ArrayList<String> items;
    private ArrayAdapter<String> itemsAdapter;
    private ListView lvItems;
    private void readItems() {
        File filesDir = getFilesDir();
        File todoFile = new File(filesDir, "todo.txt");
        try {
            items = new ArrayList<String>(FileUtils.readLines(todoFile));
        } catch (IOException e) {
            items = new ArrayList<String>();
        }
    }
    private void writeItems() {
        File filesDir = getFilesDir();
        File todoFile = new File(filesDir, "todo.txt");
        try {
            FileUtils.writeLines(todoFile, items);
        } catch (IOException e) {
            e.printStackTrace();
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lvItems = (ListView) findViewById(R.id.lvItems);
        readItems();
        items = new ArrayList<String>();
        itemsAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, items);
        lvItems.setAdapter(itemsAdapter);
        setupListViewListener();

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onAddItem();
            }
        });
    }

    public void onAddItem() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Add a task");
        builder.setMessage("What do you want to do?");
        final EditText inputField = new EditText(this);
        builder.setView(inputField);
        builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                String itemText = inputField.getText().toString();
                itemsAdapter.add(itemText);
                writeItems();
            }
        });
        builder.setNegativeButton("Cancel", null);
        builder.create().show();
    }
    private void setupListViewListener() {
        lvItems.setOnItemLongClickListener(
                new AdapterView.OnItemLongClickListener() {
                            @Override
                            public boolean onItemLongClick(AdapterView<?> adapter,
                                                           View item, int pos, long id) {
                                // Remove the item within array at position
                                items.remove(pos);
                                // Refresh the adapter
                                itemsAdapter.notifyDataSetChanged();
                                writeItems();
                                // Return true consumes the long click event (marks it handled)
                                return true;
                            }

                        });
            }
            @Override
            public boolean onCreateOptionsMenu(Menu menu) {
                // Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.menu.menu_main, menu);
                return true;
            }
            @Override
            public boolean onOptionsItemSelected(MenuItem item) {
                int id = item.getItemId();
                if (id == R.id.action_settings) {
                    return true;
                }
                return super.onOptionsItemSelected(item);
            }
        }

答案 1 :(得分:0)

您应该将SQL字符串传递给prepare,因此执行query调用无效:

// skip next line
//$results = $link->query("SELECT code FROM code WHERE code = :code");
$query_params = array( 
    ':code' => $_POST['code']
);

// Use SQL as argument here, not $results
$stmt = $link->prepare("SELECT code FROM code WHERE code = :code");